<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BSTimeslot
 *
 * @author jianfeng
 */
class BSTimeslot extends BSEntity{
    //put your code here
    public $name = 'no-name';
    public $cronValue = "no value";
    public $description = 'no-description';
    public $status = 0;
    public $spaceId = 0;
    public $appId = 0;
    
    public function getPropertyToColumnMapList($data = NULL) {
        $list = parent::getPropertyToColumnMapList($data);
        $list['cronValue'] = 'cron_value';
        return $list;
    }
    
     public function initBeforeLoad($data = NULL) {
        if ($data != 'notInitBeforeLoad') {
            parent::initBeforeLoad($data);
            self::initInput();
        }
    }

    public static function initInput($data = NULL) {
        $spaceId = Blyn::app()->getActiveSpace()->getId();
        $appId = Blyn::app()->getActiveApp()->getId();
        
        $r = new BSTimeslot('notInitBeforeLoad');
        $r->name = 'workingdays-normal';
        $r->cronValue = '0 8-23 * * 1-5';
        $r->description = 'time slot on working days in normal period';
        $r->spaceId = $spaceId;
        $r->appId = $appId;
        
        $r->save();
//        BPermit::addPermit($r, BPermit::AllowModify, Blyn::app()->getActiveApp()->getRole(BRole::Role_Space_Admin), array('userId' => $creatorId));

        $r = new BSTimeslot();
        $r->name = 'weekend-normal';
        $r->cronValue = '0 10-22 * * 6-7';
        $r->description = 'time slot on weekend days in normal period';
        $r->spaceId = $spaceId;
        $r->appId = $appId;
        $r->save();
//        BPermit::addPermit($r, BPermit::AllowModify, Blyn::app()->getActiveApp()->getRole(BRole::Role_Space_Admin), array('userId' => $creatorId));
    }

    public function save($context = null) {

        if ($this->spaceId <= 0)
            $this->spaceId = Blyn::app()->getActiveSpace()->getId();
        if ($this->appId <= 0)
            $this->appId = $appId = Blyn::app()->getActiveApp()->getId();

        parent::save($context);
    }

}
